/**
 * Created by Administrator on 2017/8/15.
 */

var mysql = require('mysql');
var pool = mysql.createPool({
    host: 'rm-bp1272001633qc0x9.mysql.rds.aliyuncs.com',
    user: 'hexin',
    password: 'gYUHszn9#q',
    database: 'node_dsgjj3'
});
var localPool = mysql.createPool({
    host: '127.0.0.1',
    user: 'root',
    password: 'root',
    database: 'node_gjj'
});

function getquery(sql, callback) {
    localPool.getConnection(function (err, connection) {
        // Use the connection
        if(err){
            console.error(err);
            return;
        }
        connection.query(sql, function (err, rows) {
            callback(err, rows);
            connection.release();//释放链接
        });
    });
}
function query(sql, post, callback) {
    //链接
    localPool.getConnection(function (err, connection) {
        // Use the connection
        if (err) {
            console.error(err);
            return;
        }
        //查询
        connection.query(sql, post, function (err, rows) {
            callback(err, rows);
            connection.release();//释放链接
        });
    });
}
function getRow(sql, post, callback) {
    localPool.getConnection(function (err, connection) {
        // Use the connection
        if (err) {
            console.error(err);
            return;
        }
        connection.query(sql, post, function (err, rows) {
            if(!err){
                var row;
                if(!rows.length<1){
                    row = rows[0];
                }else{
                    row = false;
                }
                callback(false, row);
                connection.release();//释放链接
            }else{
                callback(err)
                connection.release();
            }

        });
    });
}



var nodeGjjDB = mysql.createPool({
    host: 'rm-bp1272001633qc0x9.mysql.rds.aliyuncs.com',
    user: 'hexin',
    password: 'gYUHszn9',
    database: 'node_dsgjj3'
});
var localDB = mysql.createPool({
    host: '127.0.0.1',
    user: 'root',
    password: 'root',
    database: 'node_gjj'
});

var DB = {
    query: function(sql, post, callback) {
        this['myPool'].getConnection(function (err, connection) {
            // Use the connection
            if (err) {
                console.error(err);
                return;
            }
            connection.query(sql, post, function (err, rows) {
                callback(err, rows);
                connection.release();//释放链接
            });
        });
    },
    getRow: function(sql, post, callback) {
        this['myPool'].getConnection(function (err, connection) {
            // Use the connection
            if (err) {
                console.error(err);
                return;
            }
            connection.query(sql, post, function (err, rows) {
                if(!err){
                    var row;
                    if(!rows.length<1){
                        row = rows[0];
                    }else{
                        row = false;
                    }
                    callback(false, row);
                    connection.release();//释放链接
                }else{
                    callback(err)
                    connection.release();
                }

            });
        });
    },
    getDB:function(name){
        try{
            this.myPool = eval(name);
            return this;
        }catch (error){
            console.log(name+' is undefined');
            return false;
        }
    }
}

exports.query = query;
exports.getRow = getRow;
exports.DB = DB;
